package com.wisdom.benefit.jufa.utils;

import android.graphics.drawable.Drawable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.wisdom.benefit.jufa.R;

/**
 * EditText帮助类
 * Created by zhoudawei on 2016/12/15.
 */

public final class EditTextHelper {

    private static Drawable deleteDrawable;
    private static Drawable eyeDrawablePassword;
    private static Drawable eyeDrawableText;

    public static EditTextWrapper getEditTextDeleteIconWrapper(@NonNull EditText editText) {
        final EditTextWrapper wrapper = new EditTextWrapper(editText);
        if (deleteDrawable == null)
            deleteDrawable = ContextCompat.getDrawable(editText.getContext(), R.drawable.ic_delete);
        wrapper.setDrawableRight(deleteDrawable);
        wrapper.setOnDrawableRightClickListener(() -> editText.setText(""));
        return wrapper;
    }

    public static EditTextWrapper getEditTextEyeIconWrapper(@NonNull EditText editText, @Nullable ImageView eyeImageView) {
        final EditTextWrapper wrapper = new EditTextWrapper(editText);
        if (eyeDrawablePassword == null)
            eyeDrawablePassword = ContextCompat.getDrawable(editText.getContext(), R.drawable.ic_eye_password);
        if (eyeDrawableText == null)
            eyeDrawableText = ContextCompat.getDrawable(editText.getContext(), R.drawable.ic_eye_text);
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        if (eyeImageView == null) {// 切换图标在EditText内部
            wrapper.setDrawableRight(eyeDrawablePassword);
            wrapper.setOnDrawableRightClickListener(() -> {
                final int selectionStart = editText.getSelectionStart();
                final int selectionEnd = editText.getSelectionEnd();
                if (editText.getCompoundDrawables()[2] == eyeDrawablePassword) {
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    editText.setSelection(selectionStart, selectionEnd);
                    wrapper.setDrawableRight(eyeDrawableText);
                } else {
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    editText.setSelection(selectionStart, selectionEnd);
                    wrapper.setDrawableRight(eyeDrawablePassword);
                }
            });
            return wrapper;
        } else {// 切换图标在EditText外部
            eyeImageView.setImageResource(R.drawable.ic_eye_password);
            eyeImageView.setOnClickListener(v -> {
                final int selectionStart = editText.getSelectionStart();
                final int selectionEnd = editText.getSelectionEnd();
                if (editText.getTransformationMethod() instanceof PasswordTransformationMethod) {
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    editText.setSelection(selectionStart, selectionEnd);
                    eyeImageView.setImageResource(R.drawable.ic_eye_text);
                } else {
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    editText.setSelection(selectionStart, selectionEnd);
                    eyeImageView.setImageResource(R.drawable.ic_eye_password);
                }
            });
            return getEditTextDeleteIconWrapper(editText);
        }
    }
}
